package com.example.demo1.service.servicelmpl;

import com.example.demo1.domain.Team;
import com.example.demo1.domain.User;
import com.example.demo1.repository.TeamDao;
import com.example.demo1.repository.UserDao;
import com.example.demo1.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class TeamServiceImpl implements TeamService {

    @Autowired
    private TeamDao teamDao;

    @Autowired
    private UserDao userDao;

    @Override
    public void createTeam(String teamName, String captainPhone) {
        User captain = userDao.findByPhoneCode(captainPhone);
        if (captain != null) {
            Team team = new Team();
            team.setTeamName(teamName);
            team.setMember1(captain.getUname());  // 使用 getUsername() 获取名字
            teamDao.save(team);
        }
    }

    @Override
    public void addMember(String teamName, String memberPhone) {
        Team team = teamDao.findByTeamName(teamName);
        User member = userDao.findByPhoneCode(memberPhone);

        if (team != null && member != null) {
            if (team.getMember1() == null || team.getMember1().isEmpty()) {
                team.setMember1(member.getUname());  // 使用 getUsername()
            } else if (team.getMember2() == null || team.getMember2().isEmpty()) {
                team.setMember2(member.getUname());  // 使用 getUsername()
            }else if (team.getMember3() == null || team.getMember3().isEmpty()) {
                team.setMember3(member.getUname());  // 设置第三成员
            } else if (team.getMember4() == null || team.getMember4().isEmpty()) {
                team.setMember4(member.getUname());  // 设置第四成员
            } else if (team.getMember5() == null || team.getMember5().isEmpty()) {
                team.setMember5(member.getUname());  // 设置第五成员
            }
            teamDao.save(team);
        }
    }

    @Override
    public void bindTeam(String captainPhone, String memberPhone) { //有待商榷
        User captain = userDao.findByPhoneCode(captainPhone);
        Team team = teamDao.findByTeamId(captain.getUid());
        User member = userDao.findByPhoneCode(memberPhone);

        if (team != null && member != null) {
            if (team.getMember1() == null) {
                team.setMember1(member.getUname());  // 使用 getUsername()
            } else if (team.getMember2() == null) {
                team.setMember2(member.getUname());  // 使用 getUsername()
            }
            teamDao.save(team);
        }
    }


}

